<?php

require_once "config/config.php";
$connect = mysql_connect(HOST_NAME . ":" . PORT, USER_NAME, PASS, DB_NAME);
mysql_select_db('web1', $connect);

function getDataPage($query, $min, $max, $key, $app, $company, $limit) {
    global $connect;
    $pages = array();
    if (!empty($min))
        $min = "pages.likes>= $min AND";
    else
        $min = "";
    if (!empty($max))
        $max = "pages.likes<= $max AND";
    else
        $max = "";
    if (empty($key))
        $key = "";
    if (!empty($app))
        $app = "apps.id='$app' AND";
    else
        $app = "";
    if (!empty($company))
        $company = "companies.id=$company AND";
    else
        $company = "";
    $query = "SELECT * FROM pages
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
            where $app
                  $company
                  $min
                  $max
                  pages.description LIKE '% $key %'
                  LIMIT 0,$limit";
    var_dump($query);
    $data = mysql_query($query, $connect);
    while ($row = mysql_fetch_assoc($data)) {
        $pages[] = $row;
    }
    return $pages;
}

function getDataCompany($query) {
    global $connect;
    $companys = array();
    $data = mysql_query($query, $connect);
    while ($row = mysql_fetch_assoc($data)) {
        $companys[] = $row;
    }
    return $companys;
}

function getDataApp($query) {
    global $connect;
    $apps = array();
    $data = mysql_query($query, $connect);
    while ($row = mysql_fetch_assoc($data)) {
        $apps[] = $row;
    }
    return $apps;
}

?>
